﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using MAntLibrary;

namespace MAnt3.dll
{
    public class Vienetta : IDodavatel
    {
        #region Property
        public string nextLine = "\r\n";
        string[] _DostupnostNazevArr = new string[] { "Skladem", "Není Skladem" };

        private IOproudy ioproudy;
        public IOproudy Ioproudy
        {
            get
            {
                return ioproudy;
            }
            set
            {
                ioproudy = value;
            }
        }

        public string[] GetDostupnostNazev()
        {
            return _DostupnostNazevArr;
        }
        #endregion

        #region Constructor
        public Vienetta(IOproudy io)
        {
            Ioproudy = io;
        }
        #endregion

        #region Method

        public List<ZboziNetDirect> CtiXml(XmlNode root, FileInfo file, Dodavatel dodavatel)
        {
            // nactu cislo pozice pokud je potreba
            int counter = 0;
            if (Ioproudy.ulozeno) { counter = Ioproudy.pozice; }

            ZboziNetDirect zbozi;
            List<ZboziNetDirect> result = new List<ZboziNetDirect>();

            // pruchod vnorenymi uzly korenoveho elementu
            foreach (XmlNode lNode in root.ChildNodes)
            {
                // popreskakuju pokud byl nacten rozpracovany soubor
                if (Ioproudy.ulozeno && counter > 0)
                {
                    counter--;
                    continue;
                }

                zbozi = new ZboziNetDirect();
                if (lNode.Name.Equals("SHOPITEM"))
                {
                    //ted jsem v uzlu SHOPITEM a zpracuju jednotlive hodnoty
                    XmlNodeList deti = lNode.ChildNodes;
                    foreach (XmlNode d in deti)
                    {
                        if (d.Name.Equals("ID")) { zbozi.IdZbozi = d.InnerText; zbozi.KodZbozi = d.InnerText; continue; }
                        if (d.Name.Equals("PRODUCT")) { zbozi.JmenoVyrobku = d.InnerText; continue; }
                        if (d.Name.Equals("DESCRIPTION")) { zbozi.KratkyPopis = d.InnerText; continue; }
                        if (d.Name.Equals("DESCRIPTION_LONG"))
                        {
                            string temp = ParserVienetta(d.InnerText);
                            zbozi.Popis = temp;
                            continue;
                        }
                        if (d.Name.Equals("IMGURL")) { zbozi.JmenoObrazku = d.InnerText; continue; }
                        if (d.Name.Equals("PRICE_VAT")) { zbozi.PuvodniCena = d.InnerText; zbozi.setNovaCena(dodavatel.SlevaProDodavatele); continue; }
                        if (d.Name.Equals("PARAMS"))
                        {
                            foreach (XmlNode x in d)
                            {
                                string[] temp = x.InnerText.Split(';');
                                if (temp.Length == 2)
                                {
                                    zbozi.setBarva(temp[0]);
                                    zbozi.setVelikost(temp[1]);

                                    zbozi.setVarianty(zbozi.getVelikost()[zbozi.getVelikost().Count - 1] + " - " + zbozi.getBarva()[zbozi.getBarva().Count - 1]);
                                }
                            }
                            continue;
                        }
                    }
                }
                //MainWindow.NactenySeznam.Add(zbozi);
                result.Add(zbozi);
            }

            // aktualizuji pocet polozek
            //MainWindow.PocetPolozek = MainWindow.NactenySeznam.Count;
            return result;
        }

        public string ZobrazInformace(List<ZboziNetDirect> list, int pocitadlo)
        {
            string temp = string.Empty;
            for (int i = 0; i < list[pocitadlo].getVarianty().Count; i++)
            {
                temp += list[pocitadlo].getVarianty()[i] + nextLine;
            }
            return temp;
        }

        public void UlozData(List<ZboziNetDirect> list, int pocitadlo, ref ZboziNetDirect noveZbozi)
        {
            noveZbozi.setVarianty(list[pocitadlo - 1].getVarianty());
        }

        public void GenerujParametry(string path, string znacka, List<ZboziNetDirect> ZboziNetDirList, string timeStamp)
        {
            string filename = "\\" + znacka + "-" + "Parametry" + "-" + timeStamp + ".xml";
            FileStream fs = new FileStream(path + filename, FileMode.Create);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            XmlWriter w = XmlWriter.Create(fs, settings);

            w.WriteStartDocument();
            w.WriteStartElement("parametry");
            w.WriteAttributeString("manufacturer", "netdirect");
            w.WriteAttributeString("version", "09.01");
            w.WriteAttributeString("bVlozit", "1");
            w.WriteAttributeString("bOpravit", "1");
            w.WriteAttributeString("bKompletniXml", "1");
            w.WriteAttributeString("sIdZbozi", "sKodZbozi");

            //cyklus
            for (int i = 0; i < ZboziNetDirList.Count; i++)
            {
                //cosi
                string hlavniHodnota = "1";
                int poradizobrazenihodnoty = 1;

                for (int j = 0; j < ZboziNetDirList[i].getVarianty().Count; j++)
                {
                    w.WriteStartElement("zaznam");

                    w.WriteElementString("sJmenoParametru", "velikost-barva");

                    //1
                    w.WriteElementString("sJednotkaParametru", " ");
                    w.WriteElementString("sPopisParametru", "Barva a velikost");
                    w.WriteElementString("sIdZbozi", ZboziNetDirList[i].IdZbozi);
                    w.WriteElementString("sHodnotaParametru", ZboziNetDirList[i].getVarianty()[j]); //100-bila
                    w.WriteElementString("bVypnout", "0");

                    w.WriteElementString("bHlavniHodnota", hlavniHodnota); //1
                    w.WriteElementString("nPoradiZobrazeniParametru", "100");

                    //100
                    w.WriteElementString("nPoradiZobrazeniHodnoty", poradizobrazenihodnoty.ToString()); //1
                    w.WriteElementString("bVyberovyParametr", "1");

                    w.WriteEndElement();
                    hlavniHodnota = "0";
                    poradizobrazenihodnoty++;
                }
            }

            w.WriteEndElement();
            w.WriteEndDocument();
            w.Flush();
            fs.Close();
        }
        
        #endregion

        #region HELP METHOD

        public static string ParserVienetta(string s)
        {
            string[] separatory = { "&amp;", "&lt;", "gt;", "&", "/strong", "/p", "\r", "\n", "br /", "strong" };
            string puvodni = s;
            string ciste = string.Empty;
            string[] temp;
            bool text = false;

            // orezu konec od slova Vazeni
            const string slovo = "Vážení";
            int i = puvodni.IndexOf(slovo);
            if (i > 0)
            {
                puvodni = puvodni.Substring(0, i);
            }

            // orezu konec od slova Vazeni
            const string slovo2 = "Dotazy";
            int i2 = puvodni.IndexOf(slovo2);
            if (i2 > 0)
            {
                puvodni = puvodni.Substring(0, i2);
            }

            //----------------- varianta pomoci SPLIT ----------------------------//
            temp = puvodni.Split(separatory, StringSplitOptions.RemoveEmptyEntries);
            string q = string.Empty;

            foreach (string x in temp)
            {
                if (!x.Equals("p"))
                {
                    q = x.Trim();
                    ciste += q;
                    ciste += " ";
                }
            }
            //----------------- ---------------------- ----------------------------//

            //----------------- varianta pomoci REPLACE ---------------------------//
            //foreach (string oldValue in separatory)
            //{
            //    ciste = puvodni.Replace(oldValue, "");
            //}



            return ciste.TrimStart();
        }

        #endregion


        public void VytvorXML(string destinationPath, string jmenoDodavatele, List<ZboziNetDirect> UpravenySeznam)
        {
            throw new NotImplementedException();
        }
    }
}
